package moco.blatt7.sensordemo;

import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

@SuppressLint("NewApi")
public class SensorDemo extends Activity implements SensorEventListener {
	
	private SensorManager sm;
	private TextView tv, light, point;	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sensor_demo);
        
        sm = (SensorManager) getSystemService(SENSOR_SERVICE);
        sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        sm.getDefaultSensor(Sensor.TYPE_LIGHT);
        
        tv = (TextView) findViewById(R.id.tv);
        light = (TextView) findViewById(R.id.light);
        light.setTextColor(getResources().getColor(android.R.color.white));
        point = (TextView) findViewById(R.id.point);
        point.setBackgroundColor(getResources().getColor(android.R.color.black));
        
    }

    @Override
    protected void onResume() {
    	super.onResume();
    	sm.registerListener(this,
    	        sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
    	        SensorManager.SENSOR_DELAY_NORMAL);
    	
    	sm.registerListener(this,
    	        sm.getDefaultSensor(Sensor.TYPE_LIGHT),
    	        SensorManager.SENSOR_DELAY_NORMAL);
    }
    @Override
    protected void onStop() {
    	super.onStop();
    	sm.unregisterListener(this);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_sensor_demo, menu);
        return true;
    }

	public void onAccuracyChanged(Sensor sensor, int accuracy) {
	}

	public void onSensorChanged(SensorEvent event) {
		if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
			_getAccelerometerValues(event);
		}
		if(event.sensor.getType() == Sensor.TYPE_LIGHT) {
			_getLightValues(event);
		}
		
	}
	
	private void _getAccelerometerValues(SensorEvent event){
		float[] values = event.values;
		
		float x = values[0];
		float y = values[1];
		float z = values[2];
		
		String str = "x: " + x + "\n";
		str += "y: " + y + "\n";
		str += "z: " + z;
		
		tv.setText(str);
		point.setX(Math.abs(x)*80);
		point.setY(Math.abs(y)*80);
		
	}
	
	private void _getLightValues(SensorEvent event){
		float light_value = event.values[0];
		
		light.setText("Licht: " + light_value);
		
		if(light_value < 20) {
			light.setBackgroundColor(Color.rgb(0, 0, 0));
		} else if(light_value >= 20 && light_value <= 50) {
			light.setBackgroundColor(Color.rgb(105, 105, 105));
		} else if(light_value > 50 && light_value <= 80) {
			light.setBackgroundColor(Color.rgb(0, 0, 139));
		} else if(light_value > 80 && light_value <= 110) {
			light.setBackgroundColor(Color.rgb(255, 0, 0));
		} else if(light_value > 110 && light_value <= 140) {
			light.setBackgroundColor(Color.rgb(0, 255, 0));
		} else if(light_value > 140) {
			light.setBackgroundColor(Color.rgb(255, 255, 0));
		}
		
	}
}
